<?php

//待办事项
namespace app\v1_0\controller\company;

use app\common\model\Company;
use think\Db;

class Todo extends \app\v1_0\controller\common\Base
{
    public function save()
    {
        $id = input('post.id/d', 0, 'intval');
        $input_data=[
            'content' => input('post.content/s', '', 'trim'),
            'date' => input('post.date/s', '', 'trim'),
            'uid' =>  $this->userinfo->uid,
        ];
        if($id){
            $input_data['del']=2;
            $result = model('TodoList')
                ->allowField(true)
                ->save($input_data,[
                    'id'=>$id
                ]);
        }else{
            $result = model('TodoList')
                ->validate(true)
                ->allowField(true)
                ->save($input_data);
        }

        if (false === $result) {
            throw new \Exception(model('TodoList')->getError());
        }
        $this->ajaxReturn(200, '添加待办成功');
    }
    public function items()
    {
        $current_page = input('get.page/d', 1, 'intval');
        $pagesize = input('get.pagesize/d', 20, 'intval');
        $TodoList = model('TodoList')
            ->field('id,content,date,del')
            ->where('uid', $this->userinfo->uid)
            ->page($current_page, $pagesize)
            ->order('date DESC ,del DESC,id DESC')
            ->select();
        $data['items'] = $TodoList;
        $data['total'] = model('TodoList')->where('uid', $this->userinfo->uid)->count();
        $this->ajaxReturn(200, '获取数据成功', $data);
    }
    public function del()
    {
        $result = model('TodoList')
            ->allowField(true)
            ->save(['del'=>1], [
                'id' => input('post.id/d', 0, 'intval'),
                'uid' => $this->userinfo->uid,
            ]);
        if (false === $result) {
            throw new \Exception(model('TodoList')->getError());
        }
        $this->ajaxReturn(200, '删除成功');
    }
}
